<?php include_once './application/views/sys_admin/common/header.php'; ?>
<!-- admin_main -->
<div class="admin_main">
    <!-- Admin_top end -->
    <?php include_once './application/views/sys_admin/common/system_left.php'; ?>
    <!-- Admin_Left End -->
    <div class="admin_right">
        <div class="admin_right_title"><strong>系统管理</strong> >> 安全设置 >> 安全问题库设置</div>

        <div class="admin_right_con" style="width: 98%">

            <a  href="javascript:editpAsk();" class="addtr">添加新安全问题</a>

            <table border="0" cellspacing="1" cellpadding="0" bgcolor="#cfe6f4" width="100%">
                <tr style="color:#0068b7;">
                    <td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>序号</strong></td>
                    <td height="35" align="center" valign="middle" bgcolor="#F1F1F1" sortType = "<?php echo $sortType; ?>" order = "sort" class="sortClo"><strong>排序</strong></td>
                    <td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>安全问题</strong></td>
                    <td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>答案格式</strong></td>
                    <td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>使用次数</strong></td>
                    <td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>状态</strong></td>
                    <td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>操作</strong></td>
                </tr>
                <?php foreach ($list as $key => $info) { ?>
                    <tr>
                        <td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $key + 1; ?></td>
                        <td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><input class="askSort" style="width:40px;height:30px;text-align: center;border: 0px;" readonly="true" attrId ="<?php echo $info['id']; ?>" oldSort="<?php echo $info['sort']; ?>" value="<?php echo $info['sort']; ?>"/></td>
                        <td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $info['ask']; ?></td>
                        <td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $answerFormats[$info['answerFormat']]; ?></td>
                        <td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $info['usedNum']; ?></td>
                        <td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $info['state'] == 0 ? '已激活' : '已暂停'; ?></td>
                        <td height="35" align="center" valign="middle" bgcolor="#FFFFFF">
                            <a href="javascript:editpAsk(<?php echo $info['id']; ?>);" >修改 </a>
                            <?php if ($info['state'] == 0) { ?>
                                <a href="#" onclick="stopAndStart(<?php echo $info['id']; ?>, 1)" >暂停</a>
                            <?php } else { ?>
                                <a href="#" onclick="stopAndStart(<?php echo $info['id']; ?>, 0)" >激活</a>
                            <?php } ?>
                            <a href="#" onclick="removeAsk(<?php echo $info['id']; ?>);" >删除 </a>
                        </td>
                    </tr>
                <?php } ?>

            </table>

        </div>
        <div id="pagination" class="page" style="margin-top:10px; text-align: center;">
            <?php echo $this->pagination->create_links(); ?>
        </div>

    </div>
    <!-- Admin_Right End -->
</div>
<!-- admin_main end -->

<div id="dialog" title="系统提示" style="display:block;">
    <div id="msg" style="text-align:center;color:red">&nbsp;</div>
    <input type="hidden" name="id" id="askid" />
    <table style="line-height: 50px;" width="100%">
        <tr>
            <td width="30%">安全问题：</td>
            <td><input class="input" type="text" name="ask" id="opask" /></td>
        </tr>
        <tr>
            <td>排序：</td>
            <td><input class="input" type="text" name="sort" id="opsort" /></td>
        </tr>
        <tr>
            <td>答案格式：</td>
            <td>
                <?php foreach ($answerFormats as $key => $value ) { ?>
                <input id="answerFormat_<?php echo $key;?>" type="radio" name="answerFormat" value="<?php echo $key;?>" /><?php echo $value;?> &nbsp;
                <?php }?>
            </td>
        </tr>
    </table>
    <div style="text-align:center;">
        <input type="button" style="width: 60px; height:24px" value="关闭" onclick="$('#dialog').dialog('close');"/>
        <input type="button" style="width: 60px; height:24px" value="提交" onclick="submitAsk();" />
    </div>
</div>

<script>

    function editpAsk(id = 0) {
        $("#opask").val('');
        $("#opsort").val('');
        $("#opanswerFormat").val('');

        $("#dialog").dialog({
            height: 350,
            width: 500,
            modal: true, //蒙层（弹出会影响页面大小）
            title: id > 0 ? '修改安全问题' : '新增安全问题'
        });
        if (id > 0) {

            $("#askid").val(id);

            $.ajax({
                type: "POST",
                url: "/sys_admin/ask/getOne",
                data: "id=" + $("#askid").val() + "&ask=" + $("#opask").val() + "&sort=" + $("#opsort").val() + "&answerFormat=" + $("input[name='answerFormat']:checked").val(),
                success: function (ret) {
                    $("#opask").val(ret.askInfo.ask);
                    $("#opsort").val(ret.askInfo.sort);
                    $("#answerFormat_" + ret.askInfo.answerFormat).attr("checked", true);
                },
                dataType: "json"
            });
        }
    }

    function submitAsk() {
        var id = $("#askid").val();

        $.ajax({
            type: "POST",
            url: "/sys_admin/ask/opAskSave",
            data: "id=" + $("#askid").val() + "&ask=" + $("#opask").val() + "&sort=" + $("#opsort").val() + "&answerFormat=" + $("input[name='answerFormat']:checked").val(),
            success: function (ret) {
                $("#dialog").dialog();
                $("#msg").html(ret.msg);
                setTimeout(function () {
                    $("#dialog").dialog("close");
                }, 5000);
                window.location.reload();
            },
            dataType: "json"
        });

    }

    function removeAsk(id) {
        if (confirm('你确定要删除?')) {
            if (id > 0) {
                $.ajax({
                    type: "POST",
                    url: "/sys_admin/ask/delAsk/" + id,
                    data: "",
                    success: function (ret) {
                        alert(ret.msg);
                        window.location.reload();
                    },
                    dataType: "json"
                });
            }
        }
    }
    function stopAndStart(id, state) {
        if (confirm('你确定要做此操作码?')) {
            if (id > 0) {
                $.ajax({
                    type: "POST",
                    url: "/sys_admin/ask/stopAndStart/" + id + "/" + state,
                    data: "",
                    success: function (ret) {
                        alert(ret.msg);
                        window.location.reload();
                    },
                    dataType: "json"
                });
            }
        }
    }

    $(document).ready(function () {
        $(".askSort").dblclick(function () {
            $(this).attr({readonly: false}).css({"border-bottom": "1px solid red"});
            ;
        }).blur(function () {
            $(this).attr({readonly: true}).css({"border-bottom": "0px"});
            var oldsort = $(this).attr('oldsort');
            var val = $(this).val();
            var id = $(this).attr('attrId');
            if (oldsort != val) {
                $.ajax({
                    type: "POST",
                    url: "/sys_admin/ask/opSort/",
                    data: "id=" + id + "&sort=" + val,
                    success: function (ret) {
//                        alert(ret.msg);
                        window.location.reload();
                    },
                    dataType: "json"
                });
            }
        });

        $(".sortClo").click(function () {
            var sortType = $(this).attr('sortType');
            var order = $(this).attr('order');
            window.location.href = '/sys_admin/ask/asklist/' + order + '/' + sortType;
        })
    });


</script>
</body>
</html>
